<%--
  Created by IntelliJ IDEA.
  User: ZLF
  Date: 2020/11/12
  Time: 21:03
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<!DOCTYPE html>
<html lang="UTF-8">
<head>
    <title>众筹网</title>
    <%@ include file="include-head.jsp" %>
    <%@ include file="modal-role-add.jsp" %>
    <%@ include file="modal-role-edit.jsp" %>
    <%@ include file="modal-role-confirm.jsp" %>
    <%@ include file="modal-role-assign-auth.jsp" %>
</head>
<body>
<script type="text/javascript">
    $(function () {

        $(function () {
            // 初始化
            window.keyword = "";
            window.pageNum = 1;
            window.pageSize = 5;
            generatePage();
        });

            //点击查询后调用函数
        $("#serachByKeyword").click(function () {
                window.keyword=$("#keyword").val();
                window.pageNum=1;
             generatePage();
        });

            //点击新增后显示modal-role-add模态框
        $("#showAddModalBtn").click(function () {
            $("#addModal").modal("show");
        });
            //点击保存执行ajax
        $("#saveRoleBtn").click(function () {
            //获取值并去除前后空格
            var roleName = $.trim($("#addModal [name=roleName]").val());
            $.ajax({
                "url": "role/save",
                "type": "post",
                "data": {
                    "name": roleName
                },
                "dataType": "json",
                "success":function(response){
                    var result = response.result;
                    if(result == "SUCCESS") {
                        //关闭模态框
                        $("#addModal").modal("hide");
                        //清空模态框
                        $("#addModal [name=roleName]").val("");
                        layer.msg("操作成功！");
                        window.pageNum = 99999999;
                        generatePage();
                    }
                    if(result == "FAILED") {
                        layer.msg("操作失败！"+response.message);
                        //清空模态框
                        $("#addModal [name=roleName]").val("");
                    }
                },
                "error":function(response){
                    layer.msg(response.status+" "+response.statusText);
                    //清空模态框
                    $("#addModal [name=roleName]").val("");
                }
            });
        });

        //修改role
        //选择动态生成的表格的上级静态元素
        $("#rolePageBody").on("click", ".pencilBtn", function() {
            $("#editModal").modal("show");
            //获取修改标签的父级的同级别的前一个的内容
            var roleName = $(this).parent().prev().text();
            //因作用域不同需设置为全局id
            window.roleId = this.id;
            //回显rolename
            $("#editModal [name=roleName]").val(roleName);
        });
            //修改role的ajax请求
        $("#updateRoleBtn").click(function () {
            var roleName = $("#editModal [name=roleName]").val();
            $.ajax({
                "url": "role/update",
                "type": "post",
                "data": {
                    "id": window.roleId,
                    "name": roleName
                },
                "dataType": "json",
                "success":function(response){
                    var result = response.result;
                    if(result == "SUCCESS") {
                        $("#editModal").modal("hide");
                        layer.msg("操作成功！");
                        generatePage();
                    }
                    if(result == "FAILED") {
                        layer.msg("操作失败！"+response.message);
                    }
                },
                "error":function(response){
                    layer.msg(response.status+" "+response.statusText);
                }
            });
        });
        //删除role的ajax请求
        $("#removeRoleBtn").click(function(){
            var requestBody = JSON.stringify(window.roleIdArray);
            $.ajax({
                "url":"role/remove",
                "type":"post",
                "data":requestBody,
                "contentType":"application/json;charset=UTF-8",
                "dataType":"json",
                "success":function(response){
                    var result = response.result;
                    if(result == "SUCCESS") {
                        layer.msg("操作成功！");
                        generatePage();
                    }
                    if(result == "FAILED") {
                        layer.msg("操作失败！"+response.message);
                    }
                },
                "error":function(response){
                    layer.msg(response.status+" "+response.statusText);
                }
            });
            $("#confirmModal").modal("hide");
        });
        //调用确认删除函数并传入idlist，
        $("#rolePageBody").on("click", ".removeBtn", function() {
            var roleName = $(this).parent().prev().text();
            var roleArray = [{
                "roleId": this.id,
                "roleName": roleName
            }];
            showConfirmModal(roleArray);
        });
        //全选设置
        $("#summaryBox").click(function () {
            var currencyStatus = this.checked;
            $(".itemBox").prop("checked", currencyStatus);
        });
        //手动全选时全选框自动打勾
        $("#rolePageBody").on("click", ".itemBox", function() {
            var checkedBoxCount = $(".itemBox:checked").length;
            var totalBoxCount = $(".itemBox").length;
            $("#summaryBox").prop("checked", checkedBoxCount == totalBoxCount);
        });
        //批量删除按钮
        $("#batchRemoveBtn").click(function () {
            var roleArray = [];
            $(".itemBox:checked").each(function () {
                var roleId = this.id;
                var roleName = $(this).parent().next().text();
                roleArray.push({
                    "roleId": roleId,
                    "roleName": roleName
                });
            });
            if(roleArray.length == 0){
                layer.msg("请至少选择一个执行删除");
                return ;
            }
            showConfirmModal(roleArray);
        });
        //权限修改模态框
        $("#rolePageBody").on("click", ".checkBtn", function() {
            window.roleId = this.id;
            $("#assignModal").modal("show");
            fillAuthTree();
        });
        //发送权限修改
        $("#assignBtn").click(function () {
            var authIdArray = [];
            var zTreeObj = $.fn.zTree.getZTreeObj("authTreeDemo");
            var checkedNodes = zTreeObj.getCheckedNodes();
            for(var i = 0; i < checkedNodes.length; i++) {
                var checkedNode = checkedNodes[i];
                var authId = checkedNode.id;
                authIdArray.push(authId);
            }
            // 为了服务器端 handler 方法能够统一使用 List<Integer>方式接收数据，roleId 也存 入数组
            var requestBody = {
                "authIdArray":authIdArray,
                "roleId":[window.roleId]
            };
            requestBody = JSON.stringify(requestBody);
            //发送修改的权限
            $.ajax({
                "url": "assign/do/role/auth",
                "type": "post",
                "data": requestBody,
                "contentType": "application/json;charset=UTF-8",
                "dataType": "json",
                "success": function(response){
                    var result = response.result;
                    if(result == "SUCCESS") {
                        layer.msg("操作成功！ ");
                    } if
                    (result == "FAILED") {
                        layer.msg("操作失败！ "+response.message);
                    }
                },
                "error":function(response) {
                    layer.msg(response.status+" "+response.statusText);
                }
            });
            $("#assignModal").modal("hide");
        });
    })
</script>
<%@ include file="include-nav.jsp" %>
<div class="container-fluid">
    <div class="row">
        <%@include file="include-sidebar.jsp" %>
        <div class="col-sm-9 col-sm-offset-3 col-md-10 col-md-offset-2 main">
            <div class="panel panel-default">
                <div class="panel-heading">
                    <h3 class="panel-title"><i class="glyphicon glyphicon-th"></i> 数据列表</h3>
                </div>
                <div class="panel-body">
                    <form class="form-inline" role="form" style="float:left;">
                        <div class="form-group has-feedback">
                            <div class="input-group">
                                <div class="input-group-addon">查询条件</div>
                                <input id="keyword" class="form-control has-success" type="text" placeholder="请输入查询条件">
                            </div>
                        </div>
                        <button id="serachByKeyword" type="button" class="btn btn-warning"><i class="glyphicon glyphicon-search"></i> 查询
                        </button>
                    </form>
                    <button id="batchRemoveBtn" type="button" class="btn btn-danger" style="float:right;margin-left:10px;"><i
                            class=" glyphicon glyphicon-remove"></i> 删除
                    </button>
                    <button id="showAddModalBtn" type="button" class="btn btn-primary" style="float:right;"><i class="glyphicon glyphicon-plus"></i> 新增
                    </button>
                    <br>
                    <hr style="clear:both;">
                    <div class="table-responsive">
                        <table class="table  table-bordered">
                            <thead>
                            <tr>
                                <th width="30">#</th>
                                <th width="30"><input id="summaryBox" type="checkbox" ></th>
                                <th>名称</th>
                                <th width="100">操作</th>
                            </tr>
                            </thead>
                            <tbody id="rolePageBody">
                            </tbody>
                            <tfoot>
                            <tr >
                                <td colspan="6" align="center">
                                    <div id="Pagination" class="pagination"><!-- 这里显示分页 --></div>
                                </td>
                            </tr>
                            </tfoot>
                        </table>
                    </div>
                </div>
            </div>
        </div>
    </div>
</div>

</body>
</html>
